package com.test.mchelepesa.activity_fragment_rice.base_rice;

import static com.test.mchelepesa.activity_fragment_rice.base_rice.BaseActivity_rice.companyLogo;

import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.test.mchelepesa.bean_rice.config_info_rice.GlobalConfig_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.ThreadUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class BaseFragment_rice extends Fragment {

    protected String pageTag_rice;

    @Override
    public void onResume() {
        super.onResume();
        recordPageResumeEvent_rice();
    }

    @Override
    public void onPause() {
        super.onPause();
        recordPagePauseEvent_rice();
    }


    //页面埋点
    private void recordPagePauseEvent_rice() {
        //保存埋点
        String fragmentName = getClass().getSimpleName();
        if (!TextUtils.isEmpty(pageTag_rice)) fragmentName = pageTag_rice;
        if (!TextUtils.isEmpty(fragmentName)) {
            HttpManager_rice.getInstance().saveUserBuriedPoint((fragmentName + "_END").toUpperCase());
        }
    }

    //页面埋点
    private void recordPageResumeEvent_rice() {
        //保存埋点
        String fragmentName = getClass().getSimpleName();
        if (!TextUtils.isEmpty(pageTag_rice)) fragmentName = pageTag_rice;
        if (!TextUtils.isEmpty(fragmentName)) {
            HttpManager_rice.getInstance().saveUserBuriedPoint((fragmentName + "_START").toUpperCase());
        }
    }


    public void initLicense(View licenseView, ImageView logoIv, TextView companyNameTv){

        String logoUrl = GlobalConfig_rice.getInstance().getCompanyLogo();
        String companyName = GlobalConfig_rice.getInstance().getCompanyName();

        if (TextUtils.isEmpty(logoUrl) || TextUtils.isEmpty(companyName)){
            licenseView.setVisibility(View.GONE);
            return;
        }

        licenseView.setVisibility(View.VISIBLE);

        if (companyLogo!=null) logoIv.setImageBitmap(companyLogo);
        else {
//            ThreadUtil_rice.getInstance().runOnChildThread(() -> {
//
//            });

            try {

                URL url = new URL(logoUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                if (connection.getResponseCode()==200){
                    InputStream inputStream = connection.getInputStream();
                    companyLogo = BitmapFactory.decodeStream(inputStream);
                    ThreadUtil_rice.getInstance().runOnUiThread(() -> {
                        if (logoIv!=null) logoIv.setImageBitmap(companyLogo);
                    });
                }

                companyNameTv.setText(companyName);

            } catch (Exception e) {
                StringUtil_rice.printException(e);
            }
        }

    }


}
